package com.dlee.mvc.listener;

import java.io.InputStream;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dlee.mvc.container.BeanContainer;
import com.dlee.mvc.container.ContextEnvironment;
import com.dlee.mvc.container.PropertiesContainer;

/**
 * web容器初始化时做一些初始化工作
 * @author d.lee
 * @date 2012-7-25
 */
public class ContextLoaderListener implements ServletContextListener{
    private static Log logger = LogFactory.getLog (ContextLoaderListener.class);
    private static final String INIT_PROPERTIES_FILE = "/system.properties";

    /**
     * 用静态块加载配置资源文件
     */
    static
    {
        InputStream is = ContextLoaderListener.class.getResourceAsStream (INIT_PROPERTIES_FILE);
        if (null == is)
        {
            if (logger.isDebugEnabled ())
            {
                logger.debug ("没有找到system.properties");
            }
        }
        else
        {
            PropertiesContainer.load (is);
        }
        
    }
    
    public void contextDestroyed(ServletContextEvent paramServletContextEvent) {
        
        
    }

    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println (logger.isDebugEnabled ());
        String contextPath = servletContextEvent.getServletContext ().getContextPath ();
        ContextEnvironment contextEnvironment = new ContextEnvironment.Builder ().setContextPath (contextPath).setAnnotationScanPath (PropertiesContainer.getProperty ("scan.path")).builder ();
        BeanContainer.init (contextEnvironment);
        //EnumSet.of (DispatcherType.REQUEST,DispatcherType.FORWARD,DispatcherType.INCLUDE);
    }
}
